//
//  SomeError.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/1/28.
//

import Foundation





enum SomeError : Error {
    case ErrorType1
    case ErrorType2
    

}


enum MyError : Error{
    
    case DesTroyError
    case NormalError
    case SimpleError
    
}

//print( "should error" )
//
//throw MyError.DesTroyError
//
//print( "finish")

class S{
    func MyFunc( p:Int )throws -> Void{
        defer{
            
            print("我结束了")
        }
        
        if p>0{
            print( p )
        }else{
            throw MyError.DesTroyError
        }
    }
    func catchTest(){
        
        do {
            try MyFunc(p:0)
        } catch MyError.DesTroyError{
            print("DesTroyError")
        } catch MyError.NormalError{
            print("NormalError")
        } catch MyError.SimpleError{
            print("SimpleError")
        }catch{
            print("Error")
        }
    }
}

func a(){
    var prefix = "XT_"
    var device_name = "2XT_sksjjsssk"
    if device_name.hasPrefix( prefix ){
        print( "包含" )
    }else{
        print( "不包含" )
    }
    
}
//打印二进制数据
func data(){
    print( "start打印数据====" )
    var send: String  = "hello"
    
    let data: Data = send.data(using: .utf8)!
    
    for index in 0..<data.count{
        //let bb = data[index] && 0xff
        print( UInt8(data[index]) )
        print( "%0X".format(parameters: data[index] )  )
        
    }

    let receive: String = String(data: data, encoding: .utf8)!
    print(receive)
    
    print( "end打印数据====" )
    
    
    
    let pink: UInt32 = 0xCC6699
    let redComponent = (pink & 0xFF0000) >> 16    // redComponent 是 0xCC, 即 204
    let greenComponent = (pink & 0x00FF00) >> 8   // greenComponent 是 0x66, 即 102
    let blueComponent = pink & 0x0000FF           // blueComponent 是 0x99, 即 153
    
}


//do{
//    //
//}catch SomeError.ErrorType1{
//    print( "成绩无效" )
//}catch{
//    print( "哈哈" )
//}

//let a = SomeError.ErrorType1
//
//switch a{
//
//case  .ErrorType1:
//    print( "ErrorType1" )
//case .ErrorType2:
//    print( "ErrorType2" )
//}

enum VendingMachineError: Error {
    case invalidSelection
    case insufficientFunds(coinsNeeded: Int)
    case outOfStock
}
